package com.my.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.my.entity.AskLeave;
import com.my.entity.UserMonitor;
import com.my.service.AskLeaveService;
import com.my.service.UserMonitorService;
import com.my.service.UserService;
import com.my.util.Rest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("api")
@CrossOrigin
public class AskLeaveApiController {

    @Autowired
    private AskLeaveService service;

    @Autowired
    private UserMonitorService monitorService;

    @Autowired
    private UserService uService;

    @PostMapping("askLeave/add")
    public Rest add(@RequestBody AskLeave askLeave) {
        if(service.save(askLeave)) {
            return Rest.ok("提交成功！");
        }
        return Rest.ok("提交失败");
    }

    @GetMapping("askLeaveById")
    public Rest askLeaveById(Long userId, Long cId) {
        if(userId != null) {
            UserMonitor monitor = monitorService.getOne(new QueryWrapper<UserMonitor>().eq("user_id", userId).eq("type", 1));
            if(monitor != null) {
                userId = null;
                cId = monitor.getGradeClassId();
            }
        }
        Integer userType = null;
        if(userId != null) {
            userType = uService.getById(userId).getType();
        }
        return Rest.ok().setData("list", service.getListById(userId, userType, cId));
    }

    @PutMapping("askLeaveStatus")
    public Rest askLeave(Long id, Long approvalId, Integer status) {
        if(service.updateById(new AskLeave().setId(id).setApprovalType(2).setApprovalId(approvalId).setStatus(status))) {
            return Rest.ok("审核成功！");
        }
        return Rest.err("审核失败！");
    }

}
